package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.ISessionService;
import com.igeekhome.biz.IUnsessionService;
import com.igeekhome.pojo.CustomerService;
import com.igeekhome.pojo.Session;
import com.igeekhome.pojo.Unsession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 会话表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@Controller
@RequestMapping("/unsession")
public class UnsessionController {
    @Autowired
    private IUnsessionService iUnsessionService;

    @Autowired
    private ISessionService isessionService;

    @RequestMapping("/UnSession")
    public String un(Model model, HttpSession s, Integer current, Integer size)
    {
//        session.setAttribute("list",iSessionService.list());//
        IPage<Unsession> page=new Page<>(current,size);
        IPage<Unsession> page1 = this.iUnsessionService.page(page);
        List<Unsession> list = page1.getRecords();
        long pagesCount = page1.getPages();
        s.setAttribute("list",list);
        s.setAttribute("pagesCount",pagesCount);
        s.setAttribute("current",current);
        s.setAttribute("size",size);
        return "UnSession";
    }

    @RequestMapping("/del")
    public String del(Model model,Unsession cs, Integer current, Integer size,HttpSession session)
    {


        CustomerService customerService;
        customerService= (CustomerService) session.getAttribute("cs");
        cs.setCustomerserviceid(customerService.getCustomerserviceid());
        this.iUnsessionService.updateById(cs);
        cs= this.iUnsessionService.getById(cs.getSessionid());
        this.isessionService.save(new Session(
                cs.getSessionid(),
                cs.getCustomerserviceid(),
                cs.getCustomerid(),
                cs.getCustomername(),
                cs.getRequest(),
                cs.getCreatetime(),
                LocalDateTime.now(),
                cs.getChannel(),
                LocalDateTime.now(),
                "好评",
                "太好了",
                null,
                cs.getCustomermsgnum(),
                cs.getCustomerservicemsgnum(),
                cs.getActive()
        ));

        this.iUnsessionService.removeById(cs.getSessionid());

        IPage<Unsession> page=new Page<>(current,size);
        IPage<Unsession> page1 = this.iUnsessionService.page(page);
        List<Unsession> list = page1.getRecords();
        long pagesCount = page1.getPages();
        session.setAttribute("list",list);
        session.setAttribute("pagesCount",pagesCount);
        session.setAttribute("current",current);
        session.setAttribute("size",size);
        return  "UnSession";
    }

}

